#include "add_contact_task.h"

#include "contact_mysql.h"
#include "contact_redis.h"

#include "base/logging.h"

namespace imserver {

void AddContactTask::run() {
  for(int i = 0; i < 2; ++i) {
    if (0 == redis_->AddContact(sender_, receiver_, timestamp_)) {
      LOG_INFO("redis AddContact ok " << sender_ << '/' << receiver_);
      break;
    }
    LOG_WARN("redis AddContact err " << sender_ << '/' << receiver_);
  }
  int ret = mysql_->Insert(sender_, receiver_, timestamp_);
  if (ret == 0) {
    LOG_INFO("mysql AddContact ok " << ret << ' ' << sender_ << '/' << receiver_);
  } else {
    LOG_WARN("mysql AddContact err " << ret << ' ' << sender_ << '/' << receiver_);
  }
}

void AddFavoriteTask::run() {
  for(int i = 0; i < 2; ++i) {
    if (0 == redis_->AddFavorite(sender_, receiver_)) {
      LOG_INFO("redis AddFavorite ok " << sender_ << '/' << receiver_);
      break;
    }
    LOG_WARN("redis AddFavorite err " << sender_ << '/' << receiver_);
  }
  int ret = mysql_->UpdateFavorite(sender_, receiver_, timestamp_);
  if (ret == 0) {
    LOG_INFO("mysql AddContact ok " << ret << ' ' << sender_ << '/' << receiver_);
  } else {
    LOG_WARN("mysql AddContact err " << ret << ' ' << sender_ << '/' << receiver_);
  }
}

}


